package cevent.source.cloudcenter.server.service;

import cevent.source.cloudcenter.server.domain.RolesEmployee;
import cevent.source.cloudcenter.server.domain.RolesEmployeeExample;
import cevent.source.cloudcenter.server.dto.PageDto;
import cevent.source.cloudcenter.server.dto.RolesEmployeeDto;
import cevent.source.cloudcenter.server.mapper.RolesEmployeeMapper;
import cevent.source.cloudcenter.server.util.DuplicateUtil;
import cevent.source.cloudcenter.server.util.UUID8Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author cevent
 * @description 员工角色管理表服务层
 */
@Service
public class RolesEmployeeService {
    private static final Logger LOG=LoggerFactory.getLogger(RolesEmployeeService.class);
    @Resource
    private RolesEmployeeMapper rolesEmployeeMapper;

    //1.无分页列表查询：dto获取domain参数
    public List<RolesEmployeeDto> rolesEmployeeDtoList(){
        RolesEmployeeExample rolesEmployeeExample = new RolesEmployeeExample();
        rolesEmployeeExample.setOrderByClause("uni_id asc");
        List<RolesEmployee> rolesEmployeeList=rolesEmployeeMapper.selectByExample(rolesEmployeeExample);
        List<RolesEmployeeDto> rolesEmployeeDtoList = new ArrayList<RolesEmployeeDto>();
        for(int i=0;i<rolesEmployeeList.size();i++){
            RolesEmployee rolesEmployee=rolesEmployeeList.get(i);
            RolesEmployeeDto rolesEmployeeDto=new RolesEmployeeDto();
            //复制拷贝(source,target)
            BeanUtils.copyProperties(rolesEmployee,rolesEmployeeDto);
            rolesEmployeeDtoList.add(rolesEmployeeDto);
        }
        return rolesEmployeeDtoList;
    }

    /*2.分页查询：pageDto保存数据
        list数据集合：listPageData
        当前页码：currentPage
        每页行数：size
        总计函数：totalSize
        前端可返回pageDto，无需返回值
     */
    public void getRolesEmployeePageDtoList(PageDto pageDto){
        //1.分页设置（前端传入当前页和每页行数params），起始页，每页行数size
        PageHelper.startPage(pageDto.getCurrentPage(),pageDto.getSize());

        RolesEmployeeExample rolesEmployeeExample=new RolesEmployeeExample();


        List<RolesEmployee> rolesEmployeeList=rolesEmployeeMapper.selectByExample(rolesEmployeeExample);
        //2.将mapper查询结果复制到pageInfo
        PageInfo<RolesEmployee> pageInfo=new PageInfo<>(rolesEmployeeList);
        //3.pageInfo计算处理处理=>后分页的总行数set到pageDto做记录
        pageDto.setTotalSize(pageInfo.getTotal());

        List<RolesEmployeeDto> rolesEmployeeDtoList=new ArrayList<>();
        rolesEmployeeList.stream().forEach(rolesEmployee->{
            RolesEmployeeDto rolesEmployeeDto=new RolesEmployeeDto();
            BeanUtils.copyProperties(rolesEmployee,rolesEmployeeDto);
            rolesEmployeeDtoList.add(rolesEmployeeDto);
        });

        pageDto.setListPageData(rolesEmployeeDtoList);

    }

    //3.新增、修改rolesEmployee，将传入的id转为rolesEmployee对象本身
    public void save(RolesEmployeeDto rolesEmployeeDto){
        RolesEmployee rolesEmployee= DuplicateUtil.copy(rolesEmployeeDto,RolesEmployee.class);
        if(StringUtils.isEmpty(rolesEmployeeDto.getUniId())){
            this.insert(rolesEmployee);
        }else{
            this.update(rolesEmployee);
        }

    }

    //4.删除模块
    public void delete(String uniId){
        RolesEmployeeExample rolesEmployeeExample=new RolesEmployeeExample();
        rolesEmployeeMapper.deleteByPrimaryKey(uniId);
    }

    //5.向外暴露dto，不暴露实体类：插入数据
    private void insert(RolesEmployee rolesEmployee){
        rolesEmployee.setUniId(UUID8Util.getShortUUID());

        try {
        Date now =new Date();
        String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
        long time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime();
        int timeInt=(int)(time/1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        rolesEmployeeMapper.insert(rolesEmployee);
    }

    //6.更新模块
    private void update(RolesEmployee rolesEmployee){
        try {
            Date now =new Date();
            String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
            long time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime();
            int timeInt=(int)(time/1000);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        rolesEmployeeMapper.updateByPrimaryKey(rolesEmployee);
    }

    //7.根据roleId获取empId
    public List<RolesEmployeeDto> getEmpsByRoleId(String roleId){
        RolesEmployeeExample rolesEmployeeExample=new RolesEmployeeExample();
        rolesEmployeeExample.createCriteria().andRolesIdEqualTo(roleId);
        List<RolesEmployee> rolesEmployees=rolesEmployeeMapper.selectByExample(rolesEmployeeExample);
        return DuplicateUtil.copyList(rolesEmployees,RolesEmployeeDto.class);
    }
}
